layui.config({
    base: '/static/admin/js/' //静态资源所在路径
}).use(['jquery', 'layer', 'dialog'], function() {
    var layer = layui.layer,
        $ = layui.jquery,
        dialog = layui.dialog;

    $(".upload").on("click", function() {
        console.log("上传");
        var $space = $(this).prev().prev().prev();
        var $modify = $(this).next().next();
        var type = $(this).data("type");

        var formData = new FormData();
        formData.append("file", $(this).prev()[0].files[0]);
        formData.append("type", type);
        formData.append("inventoryId", $(this).data("id"));

        var url = $("form").attr("action");
        formData.append("mode", "jsonp");

        var rec = $(this).next().next().children();
        var recv = rec.val();

        if (recv == "") {
            layer.msg("请填写更改内容");
            return false;
        }

        if (recv != undefined) {
            formData.append("record", recv);
        }

        $.ajax({
            url : '/system/user/checkSpace',
            type : 'get',
            dataType : "json",
            cache : false,
            processData : false,
            contentType : false,
            success : function(data) {
                if (data.rs == true) {
                    var index = 0;
                    $.ajax({
                        url : encodeURI(url),
                        type : 'post',
                        data : formData,
                        dataType : "json",
                        cache : false,
                        processData : false,
                        contentType : false,
                        beforeSend : function() {
                            index = layer.load(1, {
                                shade : [ 0.1, '#fff' ]
                            });
                        },
                        success : function(data) {
                            layer.close(index);
                            if (!jQuery.isEmptyObject(data)) {
                                if ($.trim(data.msg) != "") {
                                    layer.msg(data.msg);
                                } else {
                                    layer.msg("上传成功");
                                    var str = '<a href="javascript:void(0);" class="download" data-type="' + type + '" data-id="' + data.id + '">' + data.name + '</a>&nbsp; ';
                                    str += '<a data-url="' + $("#url").val() + '/project/project/delete_' + type + '_file/' + data.id + '" class="layui-btn layui-btn-xs layui-btn-normal hidden-xs delFile"><i class="layui-icon">&#xe640;</i></a>';
                                    $space.html(str);
                                    $modify.html('<input placeholder="更改内容" type="text" name="record" class="layui-input" value=""/>');
                                }
                            }
                        },
                        error : function() {
                            layer.msg("服务器内部错误");
                        }
                    });
                } else {
                    layer.msg("存储空间不足，请联系管理员");
                }
            },
            error : function() {
                layer.msg("服务器内部错误");
            }
        });
    });

    $(".layui-table").on("click", ".download", function() {
        var act = $("form").attr("action");
        var url = $("#url").val() + "/project/project/download_" + $(this).data("type") + "_file/" + $(this).data("id");
        $("form").attr("action", url).submit();
        $("form").attr("action", act);
    });

    $(".layui-table").on("click", ".delFile", function() {
        var url = $(this).attr('data-url');
        dialog.confirm({
            message:'你确定要删除吗？',
            success:function(){
                $.ajax({
                    url : encodeURI(url),
                    type : 'get',
                    dataType : "json",
                    cache : false,
                    processData : false,
                    contentType : false,
                    beforeSend : function() {
                        /*index = layer.load(1, {
                            shade : [ 0.1, '#fff' ]
                        });*/
                    },
                    success : function(data) {
                        if (data.success) {
                            window.location.reload();
                        }
                    },
                    error : function() {
                        layer.msg("服务器内部错误");
                    }
                });
            },
            cancel:function(){
            }
        })
    });

    $(".layui-table").on("click", ".changefile", function() {
        var type = $(this).data("type");
        var id = $(this).data("id");
        var activity = $(this).data("activity");
        var section = $(this).data("section");
        var url = $(this).data("url");
        console.log(type);
        console.log(id);
        console.log(activity);
        console.log(section);
        console.log(url);
        var temp = document.createElement("form");
        temp.action = url;
        temp.method = "post";
        var addtype = document.createElement("input");
        addtype.name = "addtype";
        addtype.value = type;
        addtype.type = "hidden";
        temp.appendChild(addtype);
        var addsection = document.createElement("input");
        addsection.name = "addsection";
        addsection.value = section;
        addsection.type = "hidden";
        temp.appendChild(addsection);
        var addactivity = document.createElement("input");
        addactivity.name = "addactivity";
        addactivity.value = activity;
        addactivity.type = "hidden";
        temp.appendChild(addactivity);
        var addinventory = document.createElement("input");
        addinventory.name = "addinventory";
        addinventory.value = id;
        addinventory.type = "hidden";
        temp.appendChild(addinventory);
        var addproject = document.createElement("input");
        addproject.name = "addproject";
        addproject.value = $("#projectId").val();
        addproject.type = "hidden";
        temp.appendChild(addproject);
        document.body.appendChild(temp);
        temp.submit();
    });

    $(":button.project-file").on("click", function() {
        var id = $(this).attr("id");
        var state = false;
        if (id == "save" || id == "update") {
            var act = $("form").attr("action");
            $("form").attr("action", act.substring(0, act.lastIndexOf("/") + 1) + id);
            if ($("input[name='record']").val() != "") {
                var $submit = $(this);
                $submit.attr("disabled", "disabled");
                $("form").submit();
                setTimeout(function() {
                    $submit.removeAttr("disabled");
                }, 1500);
            }
        } else {
            layer.confirm("确定" + $(this).val() + "吗？", {
                btn : [ "确定", "取消" ]
            }, function() {
                var act = $("form").attr("action");
                $("form").attr("action", act.substring(0, act.lastIndexOf("/") + 1) + id);
                if ($("input[name='record']").val() != "") {
                    $("form").submit();
                }
            }, function() {

            });
        }
    });

});

/*
function delRecBtn(o){
    var url = $(o).attr('data-url');
    console.log(url);
    layui.dialog.confirm({
        message:'你确定要删除吗？',
        success:function(){
            $.ajax({
                url : encodeURI(url),
                type : 'get',
                dataType : "json",
                cache : false,
                processData : false,
                contentType : false,
                beforeSend : function() {
                    /!*index = layer.load(1, {
                        shade : [ 0.1, '#fff' ]
                    });*!/
                },
                success : function(data) {
                    if (data.success) {
                        window.location.reload();
                    }
                },
                error : function() {
                    layer.msg("服务器内部错误");
                }
            });
        },
        cancel:function(){
        }
    })
    return false;
}
*/
